#include <iostream>
#include <vector>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 9000

int main() {
    // 创建服务器套接字
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == -1) {
        std::cerr << "Failed to create socket\n";
        exit(EXIT_FAILURE);
    }

    // 准备地址
    sockaddr_in server_addr {};
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(PORT);

    // 绑定地址
    if (bind(server_fd, (sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
        std::cerr << "Failed to bind address\n";
        exit(EXIT_FAILURE);
    }

    // 开始监听
    if (listen(server_fd, 3) < 0) {
        std::cerr << "Failed to start listening\n";
        exit(EXIT_FAILURE);
    }

    // 等待连接
    int client_fd;
    sockaddr_in client_addr {};
    socklen_t client_len = sizeof(client_addr);
    if ((client_fd = accept(server_fd, (sockaddr*)&client_addr, &client_len)) < 0) {
        std::cerr << "Failed to accept client\n";
        exit(EXIT_FAILURE);
    }

    // 接收参数
    std::vector<std::string> params;
    char buffer[1024];
    int bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
    while (bytes_received > 0) {
        std::string str(buffer, bytes_received);
        params.push_back(str);
        bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
    }

    // 输出参数
    std::cout << "Received parameters:\n";
    for (const auto& param : params) {
        std::cout << param << "\n";
    }

    return 0;
}